/* 
 * WTF HDR Tone Mapping & Bloom
 */

// Buffers
uniform sampler2DRect diffuseTex;
uniform sampler2DRect gSampler; // Holds bloom data

varying vec2 uv;

const float gBloomMapScale = 2.0;

// Parameters
#define SAMPLES 7
const float gExposure = 1.0;
const float gBloomEffect = 0.5;

vec4 BloomSample( in vec2 buv )
{
	return texture2DRect(gSampler, buv / gBloomMapScale);
}

void main(void) 
{
	float filter[SAMPLES] = float[SAMPLES](
		1.0/18.0, 2.0/18.0, 4.0/18.0, 4.0/18.0,
		4.0/18.0, 2.0/18.0, 1.0/18.0
	);
	
    vec4 bloomColour = vec4(0.0);
    
	float d = -(float(SAMPLES) / 2);
	for(int i = 0; i < SAMPLES; ++i)
    {
		bloomColour += BloomSample(uv + vec2(d * gBloomMapScale, 0)) * filter[i];
		bloomColour += BloomSample(uv + vec2(0, d * gBloomMapScale)) * filter[i];
		d += 1.0;
	}

    bloomColour *= gBloomEffect;
    
	gl_FragColor.rgb = texture2DRect(diffuseTex, uv).rgb * bloomColour.a + bloomColour.rgb;
    // gl_FragColor.rgb = vec3(bloomColour.a);

//	gl_FragColor = bloomColour; // DEBUG - bloom only
}

